package wqh.dao.impl;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import wqh.dao.XkDeviceDao;
import wqh.domain.XkDevice;

/**
 * Created by devts on 2017/1/5.
 */
@Repository("xkDeviceDao")
public class XkDeviceDaoImpl extends DaoImpl<XkDevice> implements XkDeviceDao {

    public XkDevice findByDeviceID(Long device_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM XkDevice as g WHERE g.device_id = :deviceID";
        Query query = session.createQuery(hql);
        query.setParameter("deviceID",device_id);
        return query.list().size() == 0 ? null : (XkDevice) query.getResultList().get(0);
    }

    public void updateIpByDeviceID(Long device_id,String ip) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "UPDATE XkDevice AS x SET x.ip = :ip WHERE x.device_id = :deviceID";
        Query query = session.createQuery(hql);
        query.setParameter("ip",ip);
        query.setParameter("deviceID",device_id);
        query.executeUpdate();
    }
}
